package org.itboys.mobile.service.mongo.mobile;

import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dto.count.KitchenCountDto;
import org.itboys.mobile.dto.score.SundayMemberFinanceDto;
import org.itboys.mobile.service.mysql.mobile.product.SundayMobileProductService;
import org.itboys.mobile.service.mysql.mobile.wallet.PearlWalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created on 2017/10/23
 * @author 朱坤涛
 * 结算。包含购物车结算和立即购买结算
 */
@Service
public class PearlMobileCountService {
    @Autowired
    private PearlMobileCartService cartService;
    @Autowired
    private PearlWalletService walletService;
    @Autowired
    private SundayMobileProductService productService;
    @Autowired
    private SundayMobileMemberService memberService;


    public KitchenCountDto countCart(Long memberId, List<Long> cartIds,Integer isUsedBanlance,Integer cartType){
        isUsedBanlance=isUsedBanlance==null?0:isUsedBanlance;
        KitchenCountDto countDto = cartService.getCart(memberId,cartIds,cartType);
        SundayMemberFinanceDto memberFinanceDto = memberService.getMemberFinance(memberId,null);
        countDto.setBalance(memberFinanceDto.getBalance());
        countDto.setDiscount(memberFinanceDto.getDiscount());

        BigDecimal totalAmount = countDto.getTotalAmount();
        BigDecimal payAmount = countDto.getPayAmount();
        //商城结算
            payAmount = payAmount.multiply(countDto.getDiscount());
           // countDto.setSendType(sendType);
            if (isUsedBanlance==1){
                countDto.setIsUserBalance(isUsedBanlance);
                BigDecimal balance = countDto.getBalance();
                if (payAmount.compareTo(balance)==1){//1.结算金额大于余额
                    countDto.setUsedBalance(balance);
                    countDto.setBalanceMsg("余额已抵扣："+balance+balance+"元");
                    payAmount = payAmount.subtract(balance);
                }else if (payAmount.compareTo(balance)==0||payAmount.compareTo(balance)==-1){ //结算金额小于余额
                    countDto.setUsedBalance(payAmount);
                    countDto.setBalanceMsg("余额已抵扣："+payAmount+"元");
                    payAmount = new BigDecimal("0.00");
                }
            }

        countDto.setTotalAmount(totalAmount);
        countDto.setPayAmount(payAmount);
        return countDto;
    }
    /**
     * 立即购买结算
     */
    public KitchenCountDto countBuyNow(Long memberId,Long productId,Integer number,Integer isUsedBalance){
        List<Long> productIds = new ArrayList<>();
        productIds.add(productId);
        List<Integer> numbers = new ArrayList<>();
        numbers.add(number);
        List<Long> cartIds = cartService.addCart(memberId,productIds,numbers,SundayCommonConstants.cart_type_buy_now);
        return countCart(memberId,cartIds,isUsedBalance,SundayCommonConstants.cart_type_buy_now);
    }
}
